import 'rxjs/add/operator/toPromise';

import {Injectable} from '@angular/core';
import {Storage} from '@ionic/storage';
import {Api} from '../api/api';

/**
 * Most apps have the concept of a User. This is a simple provider
 * with stubs for login/signup/etc.
 *
 * This User provider makes calls to our API at the `login` and `signup` endpoints.
 *
 * By default, it expects `login` and `signup` to return a JSON object of the shape:
 *
 * ```json
 * {
 *   status: 'success',
 *   data: {
 *     // User fields your app needs, like "id", "name", "email", etc.
 *   }
 * }Ø
 * ```
 *
 * If the `status` field is not `success`, then an error is detected and returned.
 */
@Injectable()
export class User {
  _user: any;

  constructor(public api: Api,
              private storage: Storage) {
    this.getUser();
  }

  /**
   * Send a POST request to our login endpoint with the data
   * the user entered on the form.
   */
  login(accountInfo: any) {
    let seq;
    if (accountInfo.type) {
      seq = this.api.post('agencies/login', accountInfo).share();
    } else {
      seq = this.api.post('customers/login', accountInfo).share();
    }
    seq.subscribe((res: any) => {
      // If the API returned a successful response, mark the user as logged in
      this._user = res;
      this._user.type = accountInfo.type;
      this.storage.set('user', this._user);
    }, err => {
      console.error('ERROR', err);
    });
    return seq;
  }


  /**
   * Log the user out, which forgets the session
   */
  logout() {
    this._user = null;
    this.storage.remove('user')
  }

  getUser() {
    return this.storage.get("user").then(res => {
      if (res) {
        this._user = res;
        return res;
      }
    })
  }

  changePassword(user: any) {
    this._user.password = user.password;
    let seq;
    if (this._user.type) {
      seq = this.api.put('agencies', this._user).share();
    } else {
      seq = this.api.put('customers', this._user).share();
    }
    seq.subscribe((res: any) => {
      // If the API returned a successful response, mark the user as logged in
      res.type = this._user.type;
      this._user = res;
      this.storage.set('user', this._user);
    }, err => {
      console.error('ERROR', err);
    });
    return seq;
  }

  changeMobile(user: any) {
    this._user.mobile = user.mobile;
    let seq = this.api.put('customers', this._user).share();
    seq.subscribe((res: any) => {
      // If the API returned a successful response, mark the user as logged in
      this._user = res;
      this.storage.set('user', this._user);
    }, err => {
      console.error('ERROR', err);
    });
    return seq;
  }

  smsLogin(mobile, code) {
    let reqParam = "?mobile=" + mobile + "&code=" + code;
    let seq = this.api.get('customers/smsLogin' + reqParam).share();
    seq.subscribe((res: any) => {
      // If the API returned a successful response, mark the user as logged in
      this._user = res;
      this.storage.set('user', this._user);
    }, err => {
      console.error('ERROR', err);
    });
    return seq;
  }

  signUp(user:any){
    let seq = this.api.post("customers", user).share();

    seq.subscribe((res:any)=>{
      console.log(res);
    },err=>{
      console.error("ERROR",err)
    });
    return seq;
  }

}
